package com.qf.controller;

import com.qf.enums.UserEnum;
import com.qf.exception.UserException;
import com.qf.pojo.User;
import com.qf.service.CategoryService;
import com.qf.service.UserService;
import com.qf.vo.ResultVo;
import com.qf.vo.ResultVoUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    CategoryService categoryService;

//    显示注册页面
    @GetMapping("/test")
    public String test(){
        return "register";
    }

//    异步校验用户名
    @PostMapping("/checkname")
    @ResponseBody
    public ResultVo checkname(String username){

        User user = userService.findByUsername(username);
        if (user==null){
            return ResultVoUtils.success();
        }
        if (user.getUsername().equals(username)){

            throw new UserException(UserEnum.PATAMS_ERROR.getCode(),"用户名重复");
        }
            return ResultVoUtils.success();

    }


//  注册提交后台验证
    @PostMapping("/register")
    @ResponseBody
    public ResultVo register(@Valid User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();
            throw new UserException(UserEnum.PATAMS_ERROR.getCode(),message );
        }
        Integer count =userService.saveMessage(user);
        if (count!=1){
            throw new UserException(UserEnum.PATAMS_ERROR.getCode(),"注册失败" );
        }
        return ResultVoUtils.success();
    }

//  直接跳转注册页面
    @GetMapping("/backregister")
    public String backregister(){
        return "register";
    }

//    异步校验登录页面
    @PostMapping("/tologin")
    @ResponseBody
    public ResultVo tologin(@Valid User user,BindingResult bindingResult,String remember){
//        进行后台参数校验
        if (bindingResult.hasErrors()){
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            throw new UserException(UserEnum.PATAMS_ERROR.getCode(), "密码错误");
        }

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(),user.getPassword() );
        if (remember!=null){
            token.setRememberMe(true);
        }

        try {
            subject.login(token);

            return ResultVoUtils.success();
        } catch (UnknownAccountException e) {
            throw new UserException(UserEnum.PATAMS_ERROR.getCode(),"用户名错误" );
        }catch (IncorrectCredentialsException e){
            throw new UserException(UserEnum.PATAMS_ERROR.getCode(),"密码错误" );
        }

    }

//    直接跳转登录页面
    @GetMapping("/login")
    public String login(){
        return "login";
    }

//直接跳转首页
    @GetMapping("/index")
    public String index(){
        return "index";
    }

//    直接登出
    @GetMapping("/logout")
    public String logout(){
        SecurityUtils.getSubject().logout();
        return "redirect:/user/index";
    }






}
